const app =getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabstatus:true,

    page:1,
    limit:15,
    readlist:[],
    bookbag:'',
    addtime:'',
    historylist:[],



    // 音乐 // 列表哪一个
    // number: '-1',

    // // 显示播放暂停
    // status: true,

    // statustime: '00:00',
    // sumtime: '',
    // width: '0%',
    // music: '',

    list: [],
    music: '',
    number: '-1',


    starttime: '00:00',
    endtime: '',
    musicname: ''
  },
  reading:function(){
    let that = this
    that.setData({
      tabstatus: true,
    })
    that.readlist()
  },
  record:function(){
    let that = this
    that.setData({
      tabstatus: false,
    })
    that.historylist()
  },

    // copy-------------
  delete: function () {
    this.setData({
      name: ''
    })
    this.musiclist()
  },
  // goaudio: function (e) {
  //   wx.navigateTo({
  //     url: '../Amusicdetails/index',
  //   })
  // },

  // name: function (e) {
  //   console.log(e)


  //   var that = this
  //   that.setData({
  //     name: e.detail.value
  //   })

  //   that.musiclist();
  // },


  // audioPlay(e) {


  //   console.log(e);
  //   this.setData({
  //     number: e.currentTarget.dataset.index,
  //     status: false,
  //     music: e.currentTarget.dataset.music,
  //     width: '0%',
  //   })
  //   console.log(this.data.number)

  //   this.innerAudioContext.destroy()

  //   if (!e.currentTarget.dataset.music) {
  //     wx.showToast({
  //       title: '暂无音频',
  //       icon: "none",
  //       duration: 2000
  //     })
  //     return false;
  //   }


    // wx.showLoading({
    //   title: '正在缓冲',
    // })
    // var that = this
    // this.innerAudioContext = wx.createInnerAudioContext()
    // this.innerAudioContext.autoplay = true
    // this.innerAudioContext.src = this.data.music
    // this.innerAudioContext.onError((res) => {
    //   console.log(res.errMsg)
    //   console.log(res.errCode)
    // })
    // setTimeout(() => {
    //   this.innerAudioContext.onTimeUpdate(() => {
    //     console.log(this.innerAudioContext.duration)  //总时长
    //     console.log(this.innerAudioContext.currentTime)  //当前播放进度
    //     let sumtime = this.innerAudioContext.duration;
    //     let newtime = this.innerAudioContext.currentTime
    //     let width = newtime / sumtime * 100 + '%'
    //     console.log(width)
    //     // 总时间
    //     let sumtimem = parseInt(sumtime / 60);
    //     let sumtimes = parseInt(sumtime % 60);


    //     // 进度时间
    //     var newtimem = parseInt(newtime / 60);
    //     var newtimes = parseInt(newtime % 60);
    //     if (newtimem < 10) {
    //       newtimem = "0" + newtimem
    //     }
    //     if (newtimes < 10) {
    //       newtimes = "0" + newtimes
    //     }


    //     let end = sumtimem + ':' + sumtimes;
    //     let start = newtimem + ':' + newtimes
    //     console.log(end)
    //     console.log(start)
    //     that.setData({
    //       width: width,
    //       sumtime: end,
    //       statustime: start,
    //     })
    //   })
    // }, 500)

    // this.innerAudioContext.onPlay(() => {
    //   wx: wx.hideLoading()
    //   that.setData({
    //     buttonstatus: false
    //   })
    // })

    //  自然播放完后
  //   this.innerAudioContext.onEnded(() => {
  //     that.setData({
  //       // 列表哪一个
  //       number: '-1',

  //       // 显示播放暂停
  //       status: true,

  //       statustime: '00:00',
  //       sumtime: '',
  //       width: '0%',
  //     })
  //   })


  // },
  // audioPause(e) {
  //   console.log(e);
  //   this.innerAudioContext.pause()
  //   this.setData({
  //     number: '-1'
  //   })


  // },
//  -----------------------------


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.readlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.innerAudioContext = wx.createInnerAudioContext()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.innerAudioContext.destroy()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    var page = that.data.page + 1
    that.setData({
      page: page
    })
    that.readlist()
    that.historylist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取列表
  readlist: function () {
    var that = this;
    if (!that.data.tabstatus){
      return false;
    }
    wx.showLoading({
      title: '加载中'
    })
    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log(res);
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.preading,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  readlist: e.data.data.goods,
                  bookbag: e.data.data.bookbag,
                  addtime: e.data.data.addtime
                })
                
                // 空的
                if (JSON.stringify(e.data.data) == "{}") {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.goods.length) {
                let readlist = that.data.readlist
                readlist = readlist.concat(e.data.data.goods)
                that.setData({
                  readlist: readlist
                })
                wx.hideLoading()
                console.log(readlist)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }


            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
      fail(e) {
        wx.showToast({
          title: '请登录',
          icon:'none',
          duration:2000,
          success(){
            setTimeout(()=>{
              wx.navigateTo({
                url: '../Adlzc/index',
              })
            },2000)
          }
        })

      }
    })
  },

  // 获取列表
  historylist: function () {
    var that = this;
    if(that.data.tabstatus) {
      return false;
    }
    wx.showLoading({
      title: '加载中'
    })

    wx.getStorage({
      key: 'puser',
      success: function (res) {
        console.log(res);
        var setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.pread_history,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  historylist: e.data.data.list,
                })
                console.log("aaa")
                wx.hideLoading();
                // 空的
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let historylist = that.data.historylist
                historylist = historylist.concat(e.data.data.list)
                that.setData({
                  historylist: historylist
                })
                wx.hideLoading()
                console.log(list)
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }


            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../login/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      },
      fail(e) {
        wx.showToast({
          title: '请等录',
          icon: 'none',
          duration: 2000,
          success() {
            wx.reLaunch({
              url: '../Adlzc/index',
            })
          }
        })

      }
    })
  },









  goindex: function () {
    wx.reLaunch({
      url: '../Aindex/index'
    })
  },
  gohd: function () {
    wx.reLaunch({
      url: '../Ahd/index'
    })
  },
  gomine: function () {
    wx.reLaunch({
      url: '../Amine/index'
    })
  },




  // 防止冒泡
  mp: function () {
    return false
  },
  // 点击进度条跳动
  sliderchange(event) {
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },
  sliderchanging(event) {
    console.log(event.detail)
    this.backmusic.seek(event.detail.value)
  },

  // 开始播放
  audioPlay(e) {
    console.log(e)
    let that = this

    if (!e.currentTarget.dataset.music) {
      //    wx.showToast({
      //      title:'音频地址不正确',
      //      icon:'none',
      //      duration:2000
      //    })
      return false
    }
    console.log(that.data.number)
    console.log(e.currentTarget.dataset.index)
    if (that.data.musicname == e.currentTarget.dataset.title) {
      that.setData({
        number: e.currentTarget.dataset.index,
      })
      that.backmusic.play()
      console.log("aaa")
    } else {
      console.log("bbb")
      that.setData({
        music: e.currentTarget.dataset.music,
        number: e.currentTarget.dataset.index,
        musicname: e.currentTarget.dataset.title
      })
      that.music()
    }



    that.backmusic.onTimeUpdate(() => {
      //console.log(that.backmusic.duration)  //总时长
      //console.log(that.backmusic.currentTime)//进行
      // 计算时长
      let sumtime = that.backmusic.duration;
      let newtime = that.backmusic.currentTime
      let width = newtime / sumtime * 100 + '%'
      // 总时间
      let sumtimem = parseInt(sumtime / 60);
      let sumtimes = parseInt(sumtime % 60);
      if (sumtimem < 10) {
        sumtimem = "0" + sumtimem
      }
      if (sumtimes < 10) {
        sumtimes = "0" + sumtimes
      }
      let newtimem = parseInt(newtime / 60);
      let newtimes = parseInt(newtime % 60);


      if (newtimem < 10) {
        newtimem = "0" + newtimem
      }
      if (newtimes < 10) {
        newtimes = "0" + newtimes
      }

      let end = sumtimem + ':' + sumtimes;
      let start = newtimem + ':' + newtimes
      // console.log(end)
      // console.log(start)

      that.setData({
        value: that.backmusic.currentTime,
        maxvalue: that.backmusic.duration,
        starttime: start,
        endtime: end
      })
    })

    // 循环播放
    that.backmusic.onEnded(function () {
      that.music()
    })

  },
  // 停止播放
  audioPause() {
    let that = this
    that.setData({
      number: '-1',
    })
    that.backmusic.pause()
  },



  // 创建背景音乐
  music() {
    let that = this
    that.backmusic = wx.getBackgroundAudioManager()
    that.backmusic.src = that.data.music
    that.backmusic.title = that.data.musicname
  },

  goplay: function (e) {
    let that = this
    if (that.backmusic) {
      that.backmusic.pause()
    }

    setTimeout(() => {
      that.setData({
        goplay: true
      })
    }, 2000)
    if (!that.data.goplay) {
      return false;
    }
    that.setData({
      goplay: false
    })
    wx.showLoading({
      title: '跳转中',
    })

    that.setData({
      number: '-1',
    })
    console.log(e);

    wx.setStorage({
      key: 'tmusic',
      data: e.currentTarget.dataset,
      success: function (res) {
        wx.hideLoading()
        wx.navigateTo({
          url: '../kAhbypplay/index'
        })
      },
    })
  },




})